package com.px.scserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.px.scserver.api.pc.v1.request.FileBatchMoveRequest;
import com.px.scserver.api.pc.v1.request.FileUrlPageRequest;
import com.px.scserver.api.pc.v1.request.FileUrlSaveRequest;
import com.px.scserver.api.pc.v1.vo.FileUrlVO;
import com.px.scserver.converter.FileUrlConverter;
import com.px.scserver.mapper.FileUrlMapper;
import com.px.scserver.po.FileUrl;
import com.px.scserver.service.FileUrlService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 品讯科技
 * @description 针对表【sc_file_url】的数据库操作Service实现
 * @createDate 2023-09-19 20:08:36
 */
@Slf4j
@AllArgsConstructor
@Service
public class FileUrlServiceImpl extends ServiceImpl<FileUrlMapper, FileUrl>
        implements FileUrlService {

    private final FileUrlConverter fileUrlConverter;

    @Override
    public IPage<FileUrlVO> page(FileUrlPageRequest request) {
        Page<FileUrl> page = lambdaQuery()
                .like(StringUtils.isNotBlank(request.getFileName()), FileUrl::getName, request.getFileName())
                .page(new Page<>(request.getCurrent(), request.getSize()));
        return page.convert(fileUrlConverter::converter);
    }

    @Override
    public void saveOrUpdate(FileUrlSaveRequest request) {
        FileUrl fileUrl = fileUrlConverter.converter(request);
        if (Objects.isNull(fileUrl.getId())) {
            save(fileUrl);
            return;
        }
        updateById(fileUrl);
    }

    @Override
    public void deleteByCatalogueId(Long id) {
        lambdaUpdate()
                .in(FileUrl::getCatalogueId, List.of(id))
                .remove();
    }

    @Override
    public void deleteByCatalogueIds(List<Long> ids) {
        lambdaUpdate()
                .in(FileUrl::getCatalogueId, ids)
                .remove();
    }

    @Override
    public FileUrlVO getFileById(Long id) {
        return fileUrlConverter.converter(getById(id));
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        lambdaUpdate()
                .in(FileUrl::getId, ids)
                .remove();
    }

    @Override
    public void batchMove(FileBatchMoveRequest request) {
        lambdaUpdate()
                .set(FileUrl::getCatalogueId, request.getParentId())
                .in(FileUrl::getId, List.of(request.getIds()))
                .update();
    }
}




